#!/usr/bin/env python
# $Id: test2-23.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

import re
from pyparsing import *

string1 = "2914 2828"
string2 = """
> show ip bgp 4.2.2.2
BGP routing table entry for 4.0.0.0/9
Paths: (2 available, best #2, table Default-IP-Routing-Table)
  Advertised to non peer-group peers:
  61.211.224.203 192.41.192.119
  2914 3356, (aggregated by 3356 4.69.130.6)
    59.106.248.3 from 59.106.248.23 (59.106.248.3)
      Origin IGP, metric 3100, localpref 100, valid, internal, atomic-aggregate
      Community: 9370:100
      Originator: 59.106.248.3, Cluster list: 59.106.248.23
      Last update: Wed Jun 22 16:46:23 2011

  2914 3356, (aggregated by 3356 4.69.130.6)
    59.106.248.3 from 59.106.248.22 (59.106.248.3)
      Origin IGP, metric 3100, localpref 100, valid, internal, atomic-aggregate, best 
      Community: 9370:100
      Originator: 59.106.248.3, Cluster list: 59.106.248.22
      Last update: Wed Jun 22 16:46:23 2011
"""
string3 = "2914"
string4 = "2914 3356, (aggregated by 3356 4.69.130.6)"
string5 = "59.106.248.3 from 59.106.248.23 (59.106.248.3)"

def enum(aslist):
   for ix in aslist:
      if ix:
         print(ix)

if __name__ == "__main__":
   lpar, rpar = map(Suppress, "()")
   lexer = OneOrMore(Word(nums)) + Optional(",").suppress() + Optional(alphanums)
   results = lexer.parseString(string5)
   print(results)

   """
   asinfo = []
   raspath = re.compile(r"^((\d{1,5}[\s,]*)+).*")
   match = raspath.search(string1)
   if match:
      asinfo = match.groups()[0].replace(",", "").split(" ")
      # asinfo = match.groups()[0].split(" ")

   enum(asinfo)

   for ix in string2.split("\n"):
      line = ix.strip().replace(",", "")
      match = raspath.search(line)
      if match:
         print(match.groups())
         # asinfo = match.groups()[0].replace(",", "").split(" ")
      # asinfo = match.groups()[0].split(" ")

   # enum(asinfo)
   #
   print(string4)
   match = raspath.search(string4)
   if match:
      print(match.groups())
   """

